package com.lanlian.cayman.utils;



import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;


/**
 * Created by ygh on 2018/4/19.
 */

public class TimeUtil {
    /**
     * Java将Unix时间戳转换成指定格式日期字符串
     * @param timestampString 时间戳 如："1473048265";
     *  要格式化的格式 默认："yyyy-MM-dd HH:mm:ss";
     *
     * @return 返回结果 如："2016-09-05 16:06:42";
     */
    public static String TimeStamp2Date(String timestampString) {

        String formats   = "yyyy-MM-dd HH:mm:ss";
        Long timestamp = Long.parseLong(timestampString) * 1000;
        String date = new SimpleDateFormat(formats, Locale.CHINA).format(new Date(timestamp));

        return date;
    }
    /**
     * 得到系统当前的时间戳
     * @param formats
     * @return
     */
    public static String getTimeStamp(String formats)
    {
        String cdate="";


        Date  date=new Date();
        if(formats==null) {
            formats = "yyyy-MM-dd";
        }
        cdate = new SimpleDateFormat(formats, Locale.CHINA).format(new Date());
        return  cdate;
    }
    /**
     * 得到系统当前的日期
     * @param formats
     * @return
     */
    public static String getDate(String formats)
    {
        String cdate="";


        Date  date=new Date();
        if(formats==null) {
            formats = "yyyy-MM-dd";
        }
        cdate = new SimpleDateFormat(formats, Locale.CHINA).format(new Date());
        return  cdate;
    }
    /**
     * Java将Unix时间戳转换成指定格式日期字符串
     * @param timestampString 时间戳 如："1473048265";
     *  要格式化的格式 默认："yyyy-MM-dd HH:mm:ss";
     *
     * @return 返回结果 如："2016-09-05 16:06:42";
     */
    public static String TimeStamp2Date(String timestampString, String formats ) {
        if(formats==null)
        {
            formats   = "yyyy-MM-dd HH:mm:ss";
        }

        Long timestamp = Long.parseLong(timestampString) * 1000;
        String date = new SimpleDateFormat(formats, Locale.CHINA).format(new Date(timestamp));

        return date;
    }

    /**
     * 日期格式字符串转换成时间戳
     *
     * @param dateStr 字符串日期
     *    yyyy-MM-dd HH:mm:ss
     *
     * @return
     */
    public static String Date2TimeStamp(String dateStr,String formats) {
        try {
            if(formats==null)
            {
                formats="yyyy-MM-dd HH:mm:ss";
            }
            SimpleDateFormat sdf = new SimpleDateFormat(formats);
            sdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));
            //SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
            return String.valueOf(sdf.parse(dateStr).getTime() / 1000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public static long Date2TimeStamp2(String dateStr,String formats) {
        try {
            if(formats==null)
            {
                formats="yyyy-MM-dd HH:mm:ss.SSS";
            }
            SimpleDateFormat sdf = new SimpleDateFormat(formats);
            sdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));
            //SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
            return sdf.parse(dateStr).getTime();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 日期格式字符串转换成时间戳
     *
     * @param dateStr 字符串日期
     *    yyyy-MM-dd HH:mm:ss
     *
     * @return
     */
    public static String Date2TimeStamp(String dateStr) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            sdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));
            //SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
            return String.valueOf(sdf.parse(dateStr).getTime() / 1000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public static String getMytime(String s1 )
    {
        String time="";

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
        SimpleDateFormat sdf2= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date date=sdf.parse(s1);
            time=sdf2.format(date);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        return time;
    }
}
